import { Entity } from "./Entity";

class StarEntity extends Entity {
  velocity_dim = 1;
  constructor(x: number, y: number, color: string, size: number) {
    super({
      type: "round",
      color,
    });
    this.rect.x = x;
    this.rect.y = y;
    this.rect.w = size;
    this.rect.h = size;
  }
  enterFrame() {
    this.move();
    this.draw();
  }
}

export class Star {
  list: StarEntity[] = [];
  add(x: number, y: number, color: string, size: number, velocity_dim: number, velocity_move: number) {
    const star = new StarEntity(x, y, color, size);
    const velocity = velocity_move * 1e-1;
    const angle = Math.random() * Math.PI * 2;
    const vx = velocity * Math.cos(angle);
    const vy = velocity * Math.sin(angle);
    const alpha = Math.random() * 0.2 + 0.1;
    star.vx = vx;
    star.vy = vy;
    star.alpha = alpha;
    star.velocity_dim = velocity_dim;
    this.list.push(star);
  }
  enterFrame() {
    this.list.forEach((star) => {
      star.enterFrame();
      star.alpha -= star.velocity_dim * 1e-4;
      if (star.alpha <= 0) {
        this.list = this.list.filter((o) => o !== star);
      }
    });
  }
}
